//
//  LoginCtrlViewController.m
//  Traveling
//
//  Created by 曹秀云 on 15/6/4.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LoginCtrl.h"
#import "RegisterCtr.h"
#import "MineDataTool.h"
@interface LoginCtrl()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *autoLoginBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation LoginCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"登录"];
    [self.loginBtn addTarget:self action:@selector(LoginClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn addTarget:self action:@selector(RegisterClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)LoginClick:(UIButton *)btn{
    NSString *username = self.phoneNumTextFiled.text;
    NSString *password = self.pwdTextFiled.text;
    
    if (ISBLANK(username))
    {
        [DialogUtils showTipsWithView:nil String:@"手机号不能为空！"];
        return;
    }
    
    
    if (!username.isValidatePhone)
    {
        [DialogUtils showTipsWithView:nil String:@"用户名不合法！"];
        return;
    }
    
    
    if (ISBLANK(password))
    {
        [DialogUtils showTipsWithView:nil String:@"密码不能为空！"];
        return;
    }
   
    
     MBProgressHUD *loding = [DialogUtils showLoadingWithView:self.view];
    [MineDataTool loginWithUserName:username password:password loginType:personIdentityType success:^(RegisterResult *result) {
        [loding hide:YES];
        if(result.isSucc){
            [DefaultsHelper setInteger:personIdentityType forKey:kCurrentIdentiryID];
       
          [result saveUserResult];
   
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [loding hide:YES];
        [DialogUtils showTipsWithView:nil String:@"登陆失败"];
    }];
    
}

-(void)RegisterClick:(UIButton *)btn
{
    RegisterCtr *ctrl = [[RegisterCtr alloc]init];
    [self.navigationController pushViewController:ctrl animated:YES];
}


@end
